﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Exams;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Technician;

public class CheckParameterRecord(IRepository<Exam, Guid> examRepository) : WorkflowBase, ICheckParameterRecord, ITransientDependency
{
    private readonly IRepository<Exam, Guid> _examRepository = examRepository;

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Input";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Dictionary<string, object> data)
        {
            if (Guid.TryParse(data.GetValueOrDefault("examId")?.ToString(), out Guid examId))
            {
                var exam = await _examRepository.GetAsync(examId);

                var schema = data.GetValueOrDefault("schema")?.ToString();
                if (!string.IsNullOrWhiteSpace(schema))
                {
                    exam.ExamRecordParamsSchema = schema;
                }
                data.Remove("schema");
                exam.ExamRecordParams = data.Serialize();
                //处理逻辑
                return new WorkflowOutput
                {
                    Success = true,
                    Data = data
                };
            }
        }
        return new WorkflowOutput
        {
            Success = false,
            Error = "未找到Exam对象",
        };
    }
}
